import React from 'react';
import { SearchBar } from 'antd-mobile';
import Api from 'Utils/api';
import dialog from 'Utils/dialog';
import { connect } from 'react-redux';
import * as routerActions from 'react-router-redux';
import './../NavigationPage/index.scss';
import './index.scss';

class MapSearch extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      organise: [],
      allOrganise: []
    }
  }
  componentDidMount() {
    this.handleSearchOk();
  }
  handleSearchOk(value = '') {
    const selectType = JSON.parse(sessionStorage.getItem('selectType'));
    const selectLevel = JSON.parse(sessionStorage.getItem('selectLevel'));
    const selectDept = JSON.parse(sessionStorage.getItem('selectDept'));
    const query = {};
    query.offset = 0;
    query.limit = 10000;
    if (selectDept.name) { query.manage_gov_id = selectDept.id; }
    if (selectType.name) { query.category = selectType.name; }
    if (selectLevel.name) { query.level = selectLevel.id; }
    if (value) { query.name_like = value; }
    const api = new Api({
      method: 'GET',
      path: '/enterprises/warn_level',
      query,
      success: (res) => {
        this.setState({
          organise: res.data_list
        });
      },
      error: (msg) => {
        const buttons = [
          {
            label: '知道了',
            onClick: () => {},
          },
        ];
        dialog.open('提示', msg, buttons, 'center');
      }
    });
    api.run();
  }
  handleSelect(item) {
    sessionStorage.setItem('organise', JSON.stringify(item));
    this.props.dispatch(routerActions.goBack());
  }
  render() {
    const { organise } = this.state;
    return (
      <div className="navication-search-page">
        <div className="search-wrap">
          <SearchBar
            placeholder="请输入"
            onSubmit={this.handleSearchOk.bind(this)}
            focused
          />
        </div>
        <div className="search-content">
          {
            organise.length > 0 ?
              organise.map(item => (
                <div className="search-row" key={item.id} onClick={this.handleSelect.bind(this, item)}>{item.name}</div>
              )) : <div className="result">没有匹配结果</div>
          }
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    ...state.routing,
  };
}
export default connect(mapStateToProps)(MapSearch);
